# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
from common.utils.urls import UrlUtil
from common.utils.config import ConfigUtil
from common.enums.models import ModelsEnum
from common.models.dev import DevModelsModel
from apps.admin.schemas.apply.docs import setting_schema as schema


class DocsSettingService:

    @classmethod
    async def detail(cls) -> schema.DocsSettingVo:
        """
        文档配置详情

        Returns:
            schema.DocsSettingVo: 详情配置Vo。

        Author:
            zero
        """
        config = await ConfigUtil.get("docs") or {}
        return schema.DocsSettingVo(
            status=int(config.get("status", "0")),
            auto_abstract=int(config.get("auto_abstract", "0")),
            charge_method=int(config.get("charge_method", "0")),
            charge_amount=int(config.get("charge_amount", "0")),
            ranking_status=int(config.get("ranking_status", "0")),
            ranking_models=config.get("ranking_models", ""),
            vector_models=config.get("vector_models", ""),
            chat_avatar=await UrlUtil.to_absolute_url(config.get("chat_avatar", "")),
            search_mode=str(config.get("search_mode", "similar")),
            search_tokens=int(config.get("search_tokens", 8000)),
            search_similar=float(config.get("search_similar", 0.4)),
            ranking_score=float(config.get("ranking_score", 0.4))
        )

    @classmethod
    async def save(cls, post: schema.DocsSettingIn):
        """
        文档配置详情

        Args:
            post (schema.DocsSettingIn): 配置参数。

        Author:
            zero
        """
        chat_avatar: str = UrlUtil.to_relative_url(post.chat_avatar)
        await DevModelsModel.check_models(post.vector_models, ModelsEnum.TYPE_VECTOR)
        await ConfigUtil.set("docs", "status", post.status)
        await ConfigUtil.set("docs", "auto_abstract", post.auto_abstract)
        await ConfigUtil.set("docs", "charge_method", post.charge_method)
        await ConfigUtil.set("docs", "charge_amount", post.charge_amount)
        await ConfigUtil.set("docs", "ranking_status", post.ranking_status)
        await ConfigUtil.set("docs", "ranking_models", post.ranking_models)
        await ConfigUtil.set("docs", "vector_models", post.vector_models)
        await ConfigUtil.set("docs", "chat_avatar", chat_avatar)
        await ConfigUtil.set("docs", "search_mode", post.search_mode)
        await ConfigUtil.set("docs", "search_tokens", post.search_tokens)
        await ConfigUtil.set("docs", "search_similar", post.search_similar)
        await ConfigUtil.set("docs", "ranking_score", post.ranking_score)
